<?php

use Illuminate\Database\Seeder;
use App\Permission;
use App\User;
use App\Role;

class PermissionTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        //清空权限相关的数据表
        
        DB::statement('SET FOREIGN_KEY_CHECKS = 0');

        Permission::truncate();
        Role::truncate();
        User::truncate();
        DB::table('role_user')->delete();
        DB::table('permission_role')->delete();

        DB::statement('SET FOREIGN_KEY_CHECKS = 1');

        //创建初始数据
        //创建初始的管理员用户
        $itlzk = User::create([
        	'name' => 'itlzk', 
        	'email' => '13760697742@163.com',
        	'password' => password_hash('123456', PASSWORD_DEFAULT)
        ]);

        //创建初始的角色
        $admin = Role::create([
        	'name' => 'admin',
        	'display_name' => '超级管理员',
        	'description' => 'super admin role'
        ]);

        //创建相应的初始权限
        $permissions = [
            [
                'name' => 'create_user',
                'display_name' => '创建用户',
            ],
            [
                'name' => 'edit_user',
                'display_name' => '编辑用户',
            ],
            [
                'name' => 'delete_user',
                'display_name' => '删除用户',
            ],

        ];

        foreach ($permissions as $permission) {
            $manage_user = Permission::create($permission);

            //给角色赋予相应的权限
            $admin->attachPermission($manage_user);
        }

        //给用户赋予响应的角色
        $itlzk->attachRole($admin);
        
    }
}
